package com.hr.reactive.common;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 默认订阅者订阅通用类型
 *
 * @author Administrator
 */
public class DefaultSubscriber<T> implements Subscriber<T> {
    private static final Logger log = LoggerFactory.getLogger(DefaultSubscriber.class);

    private final String name;

    public DefaultSubscriber(String name) {
        this.name = name;
    }

    @Override
    public void onSubscribe(Subscription s) {
        //默认请求最大值
        s.request(Long.MAX_VALUE);
    }

    @Override
    public void onNext(T t) {
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        log.info("{} received:{}", this.name, t);
    }

    @Override
    public void onError(Throwable t) {
        log.error("{} error", this.name, t);

    }

    @Override
    public void onComplete() {
        log.info("{} onComplete", this.name);

    }
}
